package com.xcd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.xcd.reggie.common.R;
import com.xcd.reggie.entity.Employee;
import com.xcd.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.RequestPath;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//        1、将页面提交的密码password进行md5加密处理

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(employeeLambdaQueryWrapper);

//        3、如果没有查询到则返回登录失败结果

        if (one == null){
            return  R.error("登陆失败");
        }
//        4、密码比对,如果不一致则返回登录失败结果
        if (!one.getPassword().equals(password)){
            return  R.error("登陆失败");
        }
//        5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
        if (one.getStatus() == 0){
            return  R.error("账号已被禁用");
        }
//        6、登录成功,将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        Long employee = (Long) session.getAttribute("employee");
        System.out.println(employee);
        session.removeAttribute("employee");
        return  R.success("退出成功！！！");
    }

    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        log.info("添加员工，信息如下：{}",employee.toString());
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setPassword(password);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.save(employee);
        return  R.success("添加成功");

    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {} ,pageSize = {} , name = {}",page,pageSize,name);
        //分页封装
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        //条件构造

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();

        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        //根据条件查询封装
        employeeService.page(pageInfo,lambdaQueryWrapper);

        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request) {
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        log.info("修改信息为：{}", employee.toString());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable("id") Long id) {
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
